
import path from 'path'
import fs from 'fs-extra'
import inquirer from 'inquirer'
import ini from 'ini'
import _ from 'lodash'
import ora from 'ora'
import { GMC_CONF, GMC_PATH, GMC_CONFILE, getProjectList } from './base'

export default () => {
  let project = null
  let projectList = getProjectList()
  if (projectList.length === 0) {
    console.log(`\n   还没有项目建立，先去创建一个 !\n`)
    return
  }
  return inquirer.prompt([
    {
      type: 'list',
      name: 'project',
      message: '选择一个项目 ?',
      choices: projectList
    },
    {
      type: 'inpur',
      name: 'pbDir',
      message: 'Protobuf所在目录 ?',
      validate: value => {
        if (_.isEmpty(_.trim(value))) {
          return 'Protobuf所在目录不能为空 !'
        }
        if (!fs.existsSync(path.resolve(process.cwd(), value))) {
          return `Protobuf所在目录不存在 !`
        }
        return true
      }
    }
  ])
  .then( ret => {
    let project = ret.project.replace(/(\[)([a-zA-Z]+)(\])(\s)(\-){2}(\s)(\S+)/, '$2')
    let projectConf = GMC_CONF[project]
    let inputDir = path.resolve(process.cwd(), ret.pbDir)
    let outputDir = path.resolve(GMC_PATH, projectConf.path, 'pb')
    console.log('')
    let spinner = ora(`    Import Protobuf Files ...`).start()
    fs.copySync(inputDir, outputDir)
    setTimeout(() => {
      spinner.stop()
      console.log('✔   Complete Import Protobuf Files !\n')
    }, 500)
  })
}